﻿using Microsoft.Extensions.Configuration;
using OConf.Entity;
using System.Collections.Generic;
using System.Linq;

namespace OConf.Service
{
    /// <summary>
    ///ConfigInfoHistory
    /// </summary>
    public class ConfigInfoHistoryService
    {
        private readonly IConfiguration Configuration;

        public ConfigInfoHistoryService(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        /// <summary>
		/// 是否存记录
		/// </summary>
		/// <returns>存在返回True,不存在或异常返回fasle</returns>	
		public bool Exists(string ID)
        {
            bool isExists = false;
            using (var db = new MyDBContext(Configuration))
            {
                isExists = db.ConfigInfoHistory.Any(t => t.ID == ID);
            }
            return isExists;
        }

        /// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Insert(ConfigInfoHistoryEntity model)
        {
            bool isAdd = false;
            using (var db = new MyDBContext(Configuration))
            {
                db.ConfigInfoHistory.Add(model);
                int rows = db.SaveChanges();
                isAdd = rows > 0;
            }
            return isAdd;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ConfigInfoHistoryEntity model)
        {
            bool isUpdate = false;
            using (var db = new MyDBContext(Configuration))
            {
                db.ConfigInfoHistory.Update(model);
                int rows = db.SaveChanges();
                isUpdate = rows > 0;
            }
            return isUpdate;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string ID)
        {
            bool isDelete = false;
            using (var db = new MyDBContext(Configuration))
            {
                ConfigInfoHistoryEntity model = new ConfigInfoHistoryEntity() { ID = ID };
                db.ConfigInfoHistory.Remove(model);
                int rows = db.SaveChanges();
                isDelete = rows > 0;
            }
            return isDelete;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ConfigInfoHistoryEntity GetModel(string ID)
        {
            ConfigInfoHistoryEntity model = null;
            using (var db = new MyDBContext(Configuration))
            {
                model = db.ConfigInfoHistory.Where(w => w.ID == ID).FirstOrDefault();
            }
            return model;
        }

        public List<ConfigInfoHistoryEntity> GetListByVersionId(string versionId)
        {
            List<ConfigInfoHistoryEntity> list = new List<ConfigInfoHistoryEntity>();
            using (var db = new MyDBContext(Configuration))
            {
                list = db.ConfigInfoHistory.Where(w => w.VersionID == versionId)
                    .OrderBy(o => o.ConfigKey)
                    .ToList();
            }
            return list;
        }
    }
}